﻿using System.Diagnostics.CodeAnalysis;

namespace Leetcode.N0910;
public class Solution
{
    public int SmallestRangeII(int[] nums, int k)
    {
        nums = nums.OrderByDescending(i => i).ToArray();
        var ans = nums[0] - nums[nums.Length - 1];

        for (int i = 0; i < nums.Length - 1; i++)
        {
            ans = Math.Min(ans, Math.Max(nums[0] - k, nums[i + 1] + k) - Math.Min(nums[i] - k, nums[nums.Length - 1] + k));
        }
        return ans;
    }

}